from PyQt5.QtCore import QThread
from t5_dt1ui import Ui_MainWindow


def bit_at_index(num, k):
    binary_str = bin(num)[2:].zfill(16)  # 将数字转换为无前缀的二进制字符串，并填充到16位
    return binary_str[-k] if len(binary_str) >= k else None  # 从右往左获取第k位


class serDataDeal(QThread, Ui_MainWindow):
    def __init__(self):
        super().__init__()

    def testDataToDeal(self, data):
        result = ""
        # print(bin(int(data))[2:].zfill(16))  # 打印16位二进制表示
        labels = [
            self.label_19, self.label_40, self.label_22, self.label_18,
            self.label_52, self.label_15, self.label_24, self.label_23,
            self.label_56, self.label_53, self.label_57, self.label_25
        ]
        checkBoxs = [
            self.checkBox, self.checkBox_15, self.checkBox_12, self.checkBox_2,
            self.checkBox_17, self.checkBox_3, self.checkBox_4, self.checkBox_7,
            self.checkBox_14, self.checkBox_8, self.checkBox_20, self.checkBox_13
        ]

        all_green = True  # 初始化标志变量
        # 遍历标签和位索引
        for i, (label, checkBox) in enumerate(zip(labels, checkBoxs), start=1):
            if checkBox.isChecked():  # 检查复选框是否启用
                if bit_at_index(int(data), i) == '1':  # 检查对应位是否为1
                    label.setStyleSheet("background-color: rgb(0, 255, 0);border: 1px solid black;")
                else:
                    all_green = False  # 如果有标签不是绿色，设置为 False
                    label.setStyleSheet("background-color: rgb(255, 0, 0);border: 1px solid black;")
            else:
                label.setStyleSheet("background-color: rgb(169, 169, 169);border: 1px solid black;")

        # 判断蜂鸣器的状态
        self.beeper_state = "未选择"
        if self.checkBox_5.isChecked():
            self.beeper_state = "响"
        if self.checkBox_21.isChecked():
            self.beeper_state = "不响"
        if not self.checkBox_11.isChecked(): self.beeper_state = "响"
        # 判断屏幕的状态
        self.oled_state = "未选择"
        if self.checkBox_6.isChecked():
            self.oled_state = "亮"
        if self.checkBox_22.isChecked():
            self.oled_state = "不亮"
        if not self.checkBox_18.isChecked(): self.oled_state = "亮"
        # 判断所有启用的标签是否都为绿色-判断屏幕与蜂鸣器
        if all_green and self.oled_state == "亮" and self.beeper_state == "响":
            self.result.setStyleSheet("background-color: rgb(0, 255, 0);border: 1px solid black;")
            result_text = """<html><head/><body><p align="center"><span style="font-size:36pt; font-weight:600;">PASS</span></p></body></html>"""
            self.result.setText(result_text)
            result = "pass"
        else:
            if not (all_green and self.oled_state == "亮" and self.beeper_state == "响"):
                self.result.setStyleSheet("background-color: rgb(255, 0, 0);border: 1px solid black;")
                result_text = """<html><head/><body><p align="center"><span style="font-size:36pt; font-weight:600;">error</span></p></body></html>"""
                self.result.setText(result_text)
                result = "error"
            pass
        return result
